package com.htgd.gardenexpo.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeBaseDepart;
import com.htgd.gardenexpo.entity.GeConCalendar;
import com.htgd.gardenexpo.page.DepartQuery;
import com.htgd.gardenexpo.service.GeBaseDepartService;
import com.htgd.gardenexpo.service.impl.GeBaseDepartServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 组织机构表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-03-01
 */
@RestController
@RequestMapping("/api/depart")
@Api(tags = "部门接口")
public class GeBaseDepartController {
    @Autowired
    private GeBaseDepartService geBaseDepartService;

    @ApiOperation("添加")
    @PostMapping(value = "/add")
    public CommonResult<JSONObject> add(@RequestBody GeBaseDepart geBaseDepart) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geBaseDepart.setCreateTime(LocalDateTime.now());
            geBaseDepartService.insert(geBaseDepart);
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;

    }

    @ApiOperation("查询")
    @GetMapping(value = "/list")
    public CommonResult<List<GeBaseDepart>>  list(DepartQuery departQuery) {
        CommonResult<List<GeBaseDepart>> result = new CommonResult<>();
        try {
            Page<GeBaseDepart> list=geBaseDepartService.querylist(departQuery);
            result.setData(list.getRecords());
            result.setMessage(String.valueOf(list.getTotal()));
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;

    }

    @ApiOperation("编辑")
    @PostMapping(value = "/edit")
    public CommonResult<JSONObject> edit(@RequestBody GeBaseDepart geBaseDepart) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geBaseDepart.setUpdateTime(LocalDateTime.now());
            geBaseDepartService.updateById(geBaseDepart);
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @ApiOperation("删除")
    @GetMapping(value = "/delete")
    public CommonResult<JSONObject> delete(String id) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geBaseDepartService.removeById(id);
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

}
